<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .carousel {
            margin-top: 50px;
            margin-left: 200px;
            width: 800px;
            height: 350px;
            position: relative;
        }

        .cal-list {
            width: 800px;
            height: 350px;
            position: relative;
            /* 超出不显示 */
            overflow: hidden;
        }

        .pic {
            width: 800px;
            height: 350px;
        }

        .itme {
            float: left;
        }

        .viewpage {
            position: absolute;
            left: 0;
            top: 0;
            width: 700%;
            height: 350px;
        }

        .prev,
        .next {
            width: 30px;
            height: 50px;
            font-size: 25px;
            background-color: rgba(255, 255, 255, 0.5);
            position: absolute;
            top: 43%;
            color: #fff;
            text-align: center;
            border-radius: 0 25px 25px 0;
            display: none;
        }

        .next {
            right: 0;
            border-radius: 25px 0 0 25px;
        }
    </style>
</head>

<body>
    <div class="carousel">
        <!-- 所有图片,图片列表 -->
        <div class="cal-list">
            <ul class="viewpage">
                <li class="itme"><img src="./img/one.png" class="pic"></li>
                <li class="itme"><img src="./img/two.png" class="pic"></li>
                <li class="itme"><img src="./img/three.png" class="pic"></li>
                <li class="itme"><img src="./img/four.png" class="pic"></li>
                <li class="itme"><img src="./img/five.png" class="pic"></li>
                <li class="itme"><img src="./img/six.png" class="pic"></li>
                <li class="itme"><img src="./img/one.png" class="pic"></li>
            </ul>
            <!-- 放置左右按钮 -->
            <div class="prev">&lt;</div>
            <div class="next">&gt;</div>
        </div>
    </div>
</body>
<script src="./js/animationutils.js"></script>
<script>
    var viewpage = document.getElementsByClassName('viewpage')[0];
    var itmes = document.getElementsByClassName('itme');
    var calList = document.getElementsByClassName('cal-list')[0];
    var prev = document.getElementsByClassName('prev')[0];
    var next = document.getElementsByClassName('next')[0];
    var index = 0;

    //自动轮播
    function autoplay() {
        inteID= setInterval(function () {
            index++;
            if (index >= itmes.length) {
                viewpage.style.left = '0px';
                index = 1;
            }

            var target = -800 * index;
            moveAnimation(viewpage, target);
        }, 2000)
    }
    autoplay();

    //悬停暂停.离开暂停
    calList.onmouseover=function(){
        clearInterval(inteID);
        next.style.display='block';
        prev.style.display='block';
    }
    calList.onmouseleave=function(){
        autoplay();
        next.style.display='none';
        prev.style.display='none';
    }

    // 点击下一个调用autoplay
    next.onclick=function(){
        index++;
        if (index>=itmes.length) {
            index=1;
            viewpage.style.left='0px';      
        }
        var target = -800 * index;
        moveAnimation(viewpage, target);
    }

    //点击上一个
    prev.onclick=function(){
        if (index<=0) {
            index=itmes.length-1;
            viewpage.style.left=-800*index+'px';      
        }
        index--;
        var target = -800 * index;
        moveAnimation(viewpage, target); 
    }
</script>

</html>